package itbour.onetouchshow.widget.inching;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import java.util.List;

import itbour.onetouchshow.bean.canvas.CompoundPathsBean;
import itbour.onetouchshow.bean.canvas.PathsBean;
import itbour.onetouchshow.utils.CommonUtils;


/**
 * Created by zjl_d on 2016/6/28.
 */
public class CompoundPathImageView extends View {
    private static final String TAG = "MyImageView";
    //用于计算缩放比 应该传入进来 例如 编辑界面的图片控件的宽高设置为多少就是多少
    public int vW;
    public int vH;
    public CompoundPathsBean compoundPath;
    //设置边框线的距离为
    private int mBorderWidth = 2;
    private Paint paint;
    private String colorString = "#70000000";


    public CompoundPathImageView(Context context) {
        this(context, null);
    }

    public CompoundPathImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CompoundPathImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        mBorderWidth = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, mBorderWidth, getResources()
                        .getDisplayMetrics());

    }

    public void setColorString(String colorString) {
        this.colorString = colorString;
    }

    public void setData(CompoundPathsBean compoundPath, int vW, int vH) {
        this.compoundPath = compoundPath;
        this.vW = vW;
        this.vH = vH;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        paint.setColor(Color.parseColor(colorString)); //设置画笔颜色
//        paint.setColor(Color.RED); //设置画笔颜色
        paint.setStrokeWidth(5);//设置画笔宽度
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);

        Path recentPath = new Path();

        /**
         * path每次重绘需要清空值，否则填充方式失效
         */
        recentPath.moveTo(0, 0);
        recentPath.lineTo(0, vH);
        recentPath.lineTo(vW, vH);
        recentPath.lineTo(vW, 0);
        recentPath.close();


        Path fromDataPath = new Path();
        fromDataPath.setFillType(Path.FillType.EVEN_ODD);
        fromDataPath.addPath(recentPath);


        List<PathsBean> paths = compoundPath.getPaths();
        float mW = (int) compoundPath.getW();
        float mH = (int) compoundPath.getH();
        //
        Path dataPath = CommonUtils.getTextOrShapeBesselPaths(paths, vW, vH, mW, mH);
        fromDataPath.addPath(dataPath);


        canvas.drawPath(fromDataPath, paint);


    }


}
